package com.yule.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author gjl
 * 2017/8/31
 */
@RestController
public class ConsumerController {

    @Autowired
    ConsumerService consumerService;

    @RequestMapping(value = "/service")
    public String consumer(@RequestParam("json") String json) {
        return consumerService.consumer(json);
    }

    @Service
    class ConsumerService {

        @Autowired
        RestTemplate restTemplate;

        @HystrixCommand(fallbackMethod = "fallback")
        public String consumer(String json) {
            return restTemplate.getForObject("http://SDDInterface-Server/openapi?json={1}", String.class, json);
        }
        public String fallback(String json) {
            return "{\"success\":\"0\",\"msg\":\"网络连接超时\"}";
        }

    }

}
